package com.zc.ui.effect;

import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.ParallelAction;
import com.badlogic.gdx.scenes.scene2d.actions.ScaleToAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;

public class Fade extends UIEffectBase {

	public Fade() {
	}

	public static void init(Actor actor, float duration) {

		UIEffectBase.actor = actor;
		UIEffectBase.duration = duration;

		scale = 0.3f;
		saveX = actor.getX() + actor.getParent().getX();
		saveY = actor.getY() + actor.getParent().getY();
		fromX = saveX + actor.getWidth() * (1 - scale) / 2;
		fromY = saveY + actor.getHeight() * (1 - scale) / 2;
	}

	public static void show(Actor actor, float duration) {

		init(actor, duration);

		actor.setScale(scale);
		actor.setX(fromX);
		actor.setY(fromY);
		actor.addAction(Actions.alpha(0f, 0));
		actor.setVisible(true);

		MoveToAction moveTo = Actions.moveTo(saveX, saveY, duration);
		ScaleToAction scaleTo = Actions.scaleTo(1f, 1f, duration);
		ParallelAction pa = Actions.parallel(Actions.fadeIn(duration * 2f), moveTo, scaleTo);
		Action endAction = Actions.run(new Runnable() {
			@Override
			public void run() {
				isFinish = true;
			}
		});
		SequenceAction sa = Actions.sequence(pa, endAction);
		actor.addAction(sa);
	}

	public static void hide(final Actor actor, float duration) {

		init(actor, duration);
		MoveToAction moveTo = Actions.moveTo(fromX, fromY, duration / 2);
		ScaleToAction scaleTo = Actions.scaleTo(scale, scale, duration / 2);
		ParallelAction pa = Actions.parallel(Actions.fadeOut(duration / 2), moveTo, scaleTo);
		Action endAction = Actions.run(new Runnable() {
			@Override
			public void run() {
				actor.setScale(1f);
				actor.setX(saveX);
				actor.setY(saveY);
				actor.addAction(Actions.alpha(1f, 0));
				actor.setVisible(false);
				isFinish = true;
			}
		});
		SequenceAction sa = Actions.sequence(pa, endAction);
		actor.addAction(sa);
	}

}
